package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.entity.Admin;
import com.wanmait.community.entity.AdminRole;
import com.wanmait.community.mapper.AdminMapper;
import com.wanmait.community.mapper.AdminRoleMapper;
import com.wanmait.community.mapper.RoleMapper;
import com.wanmait.community.service.AdminRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminRoleServiceImpl extends ServiceImpl<AdminRoleMapper, AdminRole> implements AdminRoleService{
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Override
    public PageInfo<AdminRole> findPage(AdminRole adminRole, Integer pageNum, Integer pageSize) {
        //先通过传进来的数据去查询对应的员工
        LambdaQueryWrapper<Admin> adminLambdaQueryWrapper = new LambdaQueryWrapper<>();
        adminLambdaQueryWrapper.like(adminRole.getAdmin().getPhone() != null,Admin::getPhone,adminRole.getAdmin().getPhone());
        List<Admin> adminList = adminMapper.selectList(adminLambdaQueryWrapper);
        List<Integer> adminIds = adminList.stream()
                .map(Admin::getId)
                .collect(Collectors.toList());
        PageHelper.startPage(pageNum, pageSize);
        //根据角色ID或员工的id去查询员工角色
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(adminRole.getRoleId() != null,AdminRole::getRoleId, adminRole.getRoleId());
        queryWrapper.in(adminIds.size() > 0,AdminRole::getAdminId,adminIds);
        queryWrapper.orderByAsc(AdminRole::getAdminId);
        List<AdminRole> adminRoleList = adminRoleMapper.selectList(queryWrapper);
        //得到的员工角色再去查询对应的员工和角色
        adminRoleList.forEach(adminRole1 -> {
            Admin admin1 = adminMapper.selectById(adminRole1.getAdminId());
            adminRole1.setAdmin(admin1);
        });
        adminRoleList.forEach(adminRole1 -> {
            if (adminRole1.getAdmin() != null
                    && Boolean.TRUE.equals(adminRole1.getAdmin().getEnable())) {
                adminRole1.setRole(roleMapper.selectById(adminRole1.getRoleId()));
            }
        });
        //返回
        return new PageInfo<>(adminRoleList, 9);
    }

    @Override
    public AdminRole getAdminRoleById(Integer adminId,Integer roleId) {
        AdminRole adminRole = new AdminRole();
        adminRole.setAdmin(adminMapper.selectById(adminId));
        adminRole.setRole(roleMapper.selectById(roleId));
        return adminRole;
    }

    @Override
    public void updateAdminRoleByAdmin(AdminRole adminRole) {
        adminRoleMapper.updateByAdminId(adminRole);
    }

}
